package com.greenantmall.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.greenantmall.AntHomeApplication;
import com.greenantmall.R;
import com.greenantmall.adapter.SelectAreaSearchAdapter;
import com.greenantmall.adapter.SelectAreaSearchAdapter.UpTownBean;
import com.greenantmall.bean.ServerSection;
import com.greenantmall.utils.Const;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.view.MSpinner;
import com.greenantmall.view.MSpinner.MSpinnerBean;
import com.greenantmall.view.MSpinner.OnMSpinnerItemClickListener;
import com.lsarah.utils.SystemBarTintManager;

public class SelectAreaSearchActivity extends BaseActivity
{
	private Button mBtnBack;
	private Button mBtnSearchClear;
	private MSpinner mCityMSpinner;
	private EditText mEtSearch;
	private TextView mTvSearch;
	private TextView mTvTopDes;
	private ListView mUpTownListView;
	private TextView mTvPrompt;
	
	private SelectAreaSearchAdapter mSelectAreaSearchAdapter;
	private List<MSpinnerBean> mCityMSpinnerBeanList;
	private List<UpTownBean> mUpTownList;
	private List<ServerSection> mServerSectionList;
	
	private String mCurSelectCity;
	
	public static final String KEY_SELECTED_SECTION = "key_selected_section";
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(v.equals(mBtnBack)) {
				SelectAreaSearchActivity.this.finish();
			}
			else if(v.equals(mBtnSearchClear)) {
				mEtSearch.setText("");
			}
			else if(v.equals(mTvSearch)) {
				if(mServerSectionList == null || mServerSectionList.size() == 0) {
					return;
				}
				
				String searchText = mEtSearch.getText().toString();
				if(TextUtils.isEmpty(searchText)) {
					UtilTools.showMyToast(SelectAreaSearchActivity.this, 
							getResources().getString(R.string.select_area_search_prompt_input_your_uptown));
					return;
				}
				
				mUpTownList.clear();
				for(ServerSection uss : mServerSectionList) {
					String city = uss.getCity();
					String uptown = uss.getUptown();
					if(mCurSelectCity.equals(city) && uptown.contains(searchText)) {
						UpTownBean upTownBean = new UpTownBean();
						upTownBean.serverSection = uss;
						mUpTownList.add(upTownBean);
					}
				}
				
				Log.d("CEXx", "CEXX ---> mUpTownList.size : " + mUpTownList.size());
				if(mUpTownList.size() == 0) {
					mUpTownListView.setVisibility(View.GONE);
		        	mTvPrompt.setVisibility(View.VISIBLE);
		        	mTvPrompt.setText(getResources().getString(R.string.select_area_search_prompt_no_uptown));
				}
				else {
					if(mSelectAreaSearchAdapter == null) {
						mSelectAreaSearchAdapter = new SelectAreaSearchAdapter(SelectAreaSearchActivity.this, mUpTownList);
				        mUpTownListView.setAdapter(mSelectAreaSearchAdapter);
					}
					else {
						mSelectAreaSearchAdapter.notifyDataSetChanged();
					}
					
					mUpTownListView.setVisibility(View.VISIBLE);
		        	mTvPrompt.setVisibility(View.GONE);
				}
			}
		}
	};
	
	private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if(arg0.equals(mUpTownListView)) {
				UpTownBean upTownBean = mUpTownList.get(arg2);
				UtilTools.saveServerSection(getApplication(), upTownBean.serverSection);
				
				Intent intent = new Intent(SelectAreaSearchActivity.this, PortalActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				SelectAreaSearchActivity.this.startActivity(intent);
				SelectAreaSearchActivity.this.finish();
				AntHomeApplication.shoppingMap.clear();
			}
		}
	};
	
	private OnMSpinnerItemClickListener mOnMSpinnerItemClickListener = new OnMSpinnerItemClickListener() {
		@Override
		public void onMSpinnerItemClick(MSpinner mspinner, int position,
				MSpinnerBean bean) {
			mCurSelectCity = bean.contentStr;
			mUpTownListView.setVisibility(View.GONE);
        	mTvPrompt.setVisibility(View.VISIBLE);
        	mEtSearch.setText("");
        	mTvTopDes.setText(mCurSelectCity);
		}
	};
	
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        SystemBarTintManager.setContentView(this, R.layout.select_area_search_activity, getResources().getColor(R.color.green));
        mBtnBack = (Button)findViewById(R.id.select_area_search_top_arrow_left);
        mBtnSearchClear = (Button)findViewById(R.id.select_area_search_cancel_icon);
        mCityMSpinner = (MSpinner)findViewById(R.id.select_area_search_top_mspinner);
        mEtSearch = (EditText)findViewById(R.id.select_area_search_et_search);
        mTvSearch = (TextView)findViewById(R.id.select_area_search_tv_search);
        mTvTopDes = (TextView)findViewById(R.id.select_area_search_top_des);
        mUpTownListView = (ListView)findViewById(R.id.select_area_search_listview);
    	mTvPrompt = (TextView)findViewById(R.id.select_area_search_tv_prompt);;
        
        mBtnBack.setOnClickListener(mOnClickListener);
        mBtnSearchClear.setOnClickListener(mOnClickListener);
        mTvSearch.setOnClickListener(mOnClickListener);
        mCityMSpinner.setTitleTextColor(getResources().getColor(android.R.color.white));
        mCityMSpinner.setTitleGravity(Gravity.CENTER);
        mCityMSpinner.setHint(getResources().getString(R.string.choose_city));
        mCityMSpinner.setOnMSpinnerItemClickListener(mOnMSpinnerItemClickListener);
        mCityMSpinner.setDisplayItemInTitle(false);
        mUpTownListView.setOnItemClickListener(mOnItemClickListener);
        
        mServerSectionList = Const.mServerSectionList;
        if(mServerSectionList == null || mServerSectionList.size() == 0) {
        	mUpTownListView.setVisibility(View.GONE);
        	mTvPrompt.setVisibility(View.VISIBLE);
        	mTvPrompt.setText(
        			getResources().getString(R.string.select_area_search_prompt_load_data_error));
        }
        else {
        	mUpTownListView.setVisibility(View.GONE);
        	mTvPrompt.setVisibility(View.VISIBLE);
        	mCityMSpinnerBeanList = new ArrayList<MSpinnerBean>();
            mUpTownList = new ArrayList<UpTownBean>();
        	
        	for(ServerSection section : mServerSectionList) {
	        	boolean have = false;
	        	for(MSpinnerBean bean : mCityMSpinnerBeanList) {
	        		if(bean.contentStr.equals(section.getCity())) {
	        			have = true;
	        			break;
	        		}
	        	}
	        	
	        	if(!have) {
	        		MSpinnerBean bean = new MSpinnerBean();
	        		bean.contentStr = section.getCity();
	        		mCityMSpinnerBeanList.add(bean);
	        	}
	        }
	        mCityMSpinner.setDropDownListData(mCityMSpinnerBeanList);
	        
	        mCurSelectCity = getIntent().getStringExtra(KEY_SELECTED_SECTION);
	        if(TextUtils.isEmpty(mCurSelectCity)) {
	        	mCurSelectCity = mCityMSpinnerBeanList.get(0).contentStr;
	        }
	        mTvTopDes.setText(mCurSelectCity);
        }
    }
}












